/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const glob = require('glob')
const autoprefixer = require('autoprefixer')
// const pxtorem = require('postcss-pxtorem')

const mockProxy = require('./mock/mock.js')

function resolve(...dir) {
  return path.join(__dirname, ...dir)
}

const projectPathRoot = process.cwd()
const viewPath = path.join(projectPathRoot, 'src/views')
const entryPath = path.join(viewPath, '**/entry.vue')
const files = glob.sync(entryPath)
const pageList = files.map(function(file) {
  const relativePath = file.slice(viewPath.length).slice(1, -10)
  return {
    relativePath
  }
})
process.env.VUE_APP_PAGE_LIST = JSON.stringify(pageList)
console.log(process.env.VUE_APP_PAGE_LIST)
console.log(process.env.NODE_ENV)
// 代理地址
const target = 'https://api.github.com'

module.exports = {
  productionSourceMap: false,
  publicPath: './',
  assetsDir: './',
  indexPath: 'index.html',
  devServer: {
    port: '8002',
    before: mockProxy,
    proxy: {
      '/api': {
        target,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    },
    disableHostCheck: true
  },
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer()
          // 如果需要rem，放开即可
          // pxtorem({
          //   rootValue: 37.5,
          //   propList: ['*']
          // })
        ]
      },
      less: {
        // 若 less-loader 版本小于 6.0，请移除 lessOptions 这一级，直接配置选项。
        lessOptions: {
          modifyVars: {
            // 直接覆盖变量
            // 'text-color': '#111',
            // 'border-color': '#eee',
            // 或者可以通过 less 文件覆盖（文件路径为绝对路径）
            hack: `true; @import "~styles/var.less";`
          }
        }
      }
    }
  },
  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('src', resolve('src'))
      .set('assets', resolve('src/assets'))
      .set('components', resolve('src/components'))
      .set('views', resolve('src/views'))
      .set('styles', resolve('src/common/styles'))
      .set('api', resolve('src/api'))
      .set('common', resolve('src/common'))
      .set('scripts', resolve('src/common/scripts'))
      .set('utils', resolve('src/common/scripts/utils'))
      .set('store', resolve('src/store'))
      .set('router', resolve('src/router'))
      .set('static', resolve('public/static'))
      .set('lang', resolve('lang'))
  }
}
